#include "globals.h"
#include "Triangle.h"

// A structure for our custom vertex type
struct CUSTOMVERTEX
{
    FLOAT x, y, z;      // The transformed position for the vertex
    DWORD color;        // The vertex color
};

// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)


void Triangle::Draw() 
{
	g_engine.getVCard()->SetTexture(0, 0);
	g_engine.getVCard()->SetStreamSource( 0, g_pVB, 0, sizeof( CUSTOMVERTEX ) );
	g_engine.getVCard()->SetFVF( D3DFVF_CUSTOMVERTEX );
	g_engine.getVCard()->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 1 );
}

//-----------------------------------------------------------------------------
// Name: Init()
// Desc: Creates a vertex buffer and fills it with our vertices. The vertex
//       buffer is basically just a chuck of memory that holds vertices. After
//       creating it, we must Lock()/Unlock() it to fill it. For indices, D3D
//       also uses index buffers. The special thing about vertex and index
//       buffers is that they can be created in device memory, allowing some
//       cards to process them in hardware, resulting in a dramatic
//       performance gain.
//-----------------------------------------------------------------------------
HRESULT Triangle::Init()
{
    // Initialize three vertices for rendering a triangle
    CUSTOMVERTEX vertices[] =
    {
        { -1.0f,-1.0f, 0.0f, 0xffff0099, },
        {  1.0f,-1.0f, 0.0f, 0xff000099, },
        {  0.0f, 1.0f, 0.0f, 0xffffff99, },
    };

    // Create the vertex buffer. Here we are allocating enough memory
    // (from the default pool) to hold all our 3 custom vertices. We also
    // specify the FVF, so the vertex buffer knows what data it contains.
    if( FAILED( g_engine.getVCard()->CreateVertexBuffer( 3 * sizeof( CUSTOMVERTEX ),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
    {
        return E_FAIL;
    }

    // Now we fill the vertex buffer. To do this, we need to Lock() the VB to
    // gain access to the vertices. This mechanism is required becuase vertex
    // buffers may be in device memory.
    VOID* pVertices;
    if( FAILED( g_pVB->Lock( 0, sizeof( vertices ), ( void** )&pVertices, 0 ) ) )
        return E_FAIL;
    memcpy( pVertices, vertices, sizeof( vertices ) );
    g_pVB->Unlock();

    return S_OK;
}

/*HRESULT Frustum::Init() 
{
	tri_vertex data[]= {
	   //Pyramid vertices
	   {-1.0f,-1.0f,-1.0f,0x0000FF00},{ 0.0f, 1.0f, 0.0f,0x00FF0000},{ 1.0f,-1.0f,-1.0f,0x000000FF},
	   { 1.0f,-1.0f,-1.0f,0x000000FF},{ 0.0f, 1.0f, 0.0f,0x00FF0000},{ 1.0f,-1.0f, 1.0f,0x0000FF00},
	   { 1.0f,-1.0f, 1.0f,0x0000FF00},{ 0.0f, 1.0f, 0.0f,0x00FF0000},{-1.0f,-1.0f, 1.0f,0x000000FF},
	   {-1.0f,-1.0f, 1.0f,0x000000FF},{ 0.0f, 1.0f, 0.0f,0x00FF0000},{-1.0f,-1.0f,-1.0f,0x0000FF00},
	   //Cube vertices
			//Front face
	   {-1.0f,-1.0f,-1.0f,0xFF0000FF},{-1.0f, 1.0f,-1.0f,0xFF0000FF},{ 1.0f, 1.0f,-1.0f,0xFF0000FF},
	   { 1.0f, 1.0f,-1.0f,0xFF0000FF},{ 1.0f,-1.0f,-1.0f,0xFF0000FF},{-1.0f,-1.0f,-1.0f,0xFF0000FF},
		  //Back face
	   { 1.0f,-1.0f, 1.0f,0xFF0000FF},{ 1.0f, 1.0f, 1.0f,0xFF0000FF},{-1.0f, 1.0f, 1.0f,0xFF0000FF},
	   {-1.0f, 1.0f, 1.0f,0xFF0000FF},{-1.0f,-1.0f, 1.0f,0xFF0000FF},{ 1.0f,-1.0f, 1.0f,0xFF0000FF},
		  //Top face
	   {-1.0f, 1.0f,-1.0f,0xFFFF0000},{-1.0f, 1.0f, 1.0f,0xFFFF0000},{ 1.0f, 1.0f, 1.0f,0xFFFF0000},
	   { 1.0f, 1.0f, 1.0f,0xFFFF0000},{ 1.0f, 1.0f,-1.0f,0xFFFF0000},{-1.0f, 1.0f,-1.0f,0xFFFF0000},
		  //Bottom face
	   { 1.0f,-1.0f,-1.0f,0xFFFF0000},{ 1.0f,-1.0f, 1.0f,0xFFFF0000},{-1.0f,-1.0f, 1.0f,0xFFFF0000},
	   {-1.0f,-1.0f, 1.0f,0xFFFF0000},{-1.0f,-1.0f,-1.0f,0xFFFF0000},{ 1.0f,-1.0f,-1.0f,0xFFFF0000},
		  //Left face
	   {-1.0f,-1.0f, 1.0f,0xFF00FF00},{-1.0f, 1.0f, 1.0f,0xFF00FF00},{-1.0f, 1.0f,-1.0f,0xFF00FF00},
	   {-1.0f, 1.0f,-1.0f,0xFF00FF00},{-1.0f,-1.0f,-1.0f,0xFF00FF00},{-1.0f,-1.0f, 1.0f,0xFF00FF00},
		  //Right face
	   { 1.0f,-1.0f,-1.0f,0xFF00FF00},{ 1.0f, 1.0f,-1.0f,0xFF00FF00},{ 1.0f, 1.0f, 1.0f,0xFF00FF00},
	   { 1.0f, 1.0f, 1.0f,0xFF00FF00},{ 1.0f,-1.0f, 1.0f,0xFF00FF00},{ 1.0f,-1.0f,-1.0f,0xFF00FF00},
	};
	void *vb_vertices;
	HRESULT hr;

	hr = g_engine.getVCard()->CreateVertexBuffer( sizeof(data),      //Length
												  D3DUSAGE_WRITEONLY,//Usage
												  tri_fvf,           //FVF
												  D3DPOOL_MANAGED,   //Pool
												  &g_pVB,        //ppVertexBuffer
												  NULL);             //Handle
	if(FAILED(hr))
	{
	  return hr;
	}

	hr=g_pVB->Lock(0, //Offset
					  0, //SizeToLock
					  &vb_vertices, //Vertices
					  0);  //Flags
	if(FAILED(hr))
	{
	  return hr;
	}

	memcpy( vb_vertices, data,sizeof(data));

	g_pVB->Unlock();


	return D3D_OK;
}*/

void Triangle::getWorldMatrix(D3DXMATRIXA16 *matWorld) 
{
	// Set up the rotation matrix to generate 1 full rotation (2*PI radians) 
    // every 1000 ms. To avoid the loss of precision inherent in very high 
    // floating point numbers, the system time is modulated by the rotation 
    // period before conversion to a radian angle.
    UINT iTime = timeGetTime() % 1000;
    FLOAT fAngle = iTime * ( 2.0f * D3DX_PI ) / 1000.0f;
    D3DXMatrixRotationY( matWorld, fAngle );
	D3DXMATRIXA16 trans;
	D3DXMatrixTranslation(&trans, 5.0f, 5.0f, 0.0f);
	D3DXMatrixMultiply(matWorld, matWorld, &trans);
	
}

//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Triangle::Cleanup()
{
    if( g_pVB != NULL )
        g_pVB->Release();
}